import datetime
import multiprocessing
from util import *
import copy
from CloudSystem import run_once


def generate_default_args():
    args=Args()
    args.method="ours"
    args.request_num=10000
    args.pre_request_num=10000
    args.spec_reliability=0.9999
    args.out_dir="output_heuristic_replica"
    args.time_expand=0.1
    args.print_level=0
    
    return args


def run_once_this(heuristic, trace_id):
    print(f"start once {heuristic}, {trace_id}")
    args=generate_default_args()
    args.heuristic_replica=heuristic
    args.trace_id=trace_id
    args_copy=copy.deepcopy(args)
    run_once(args_copy)
    print(f"End once {heuristic}, {trace_id}")
    

task_args_list=[]

for heuristic in ["down_time_resource", "up_time_resource", "down_relia", "up_relia", "down_deal_time", "up_deal_time", "down_failure_rate", "up_failure_rate","down_resource", "up_resource"]:
    for trace_id in range(10):
        task_args_list.append((heuristic, trace_id))
        
                
with multiprocessing.Pool(processes=32) as pool:
        #   starmap 直接开始所有的
        #   apply_async 一个一个参数放入执行
        
        interval=0
        results=[]
        index=0
        while index<len(task_args_list):
            # length=min(batch_size, len(task_args_list)-index )
            result=pool.apply_async(run_once_this, task_args_list[index])
            results.append(result)
            time.sleep(interval)
            index+=1
            
        for result in results:
            result.get()



print("End all sub threadings")
